import $http from '../../http/http.js'
import apiSetting from '../../http/apiSetting.js'
let dayjs = require('dayjs');
let app = getApp();

Page({
  data: {
    search: '', // 搜索框
    startDate: '',
    endDate: '',
    shade: false,
    isdaterange: false,
    proReportList: [] // 存储数据遍历
  },
  onLoad() {
    this.setData({
      startDate: dayjs().startOf('month').format('YYYY/MM/DD'),
      endDate: dayjs().endOf('month').format('YYYY/MM/DD')
    })
  },
  onShow() {
    this.productionReportPageQuery();
  },
  // 新增
  add() {
    dd.navigateTo({
      url: '/pages/productReportAdd/productReportAdd?type=add'
    })
  },
  // 编辑
  edit(e) {
    let id = e.target.dataset.item.id
    dd.navigateTo({
      url: '/pages/productReportAdd/productReportAdd?type=edit&id=' + id
    })
  },
  showMatDet() {
    this.setData({
      shade: true
    })
  },
  hiddenMatDet() {
    this.setData({
      shade: false
    })
  },
  // 处理扫码
  handleScanMethod() {
    let that = this;
    dd.scan({
      // qr:二维码,bar:条形码
      type: 'qr',
      success: (res) => {
        that.setData({
          search: res.code
        })
        that.productionReportPageQuery()
      },
    });
  },
  // 分页查询报工
  productionReportPageQuery() {
    let that = this;
    $http(apiSetting.productionReportPageQuery, {
      startTime: dayjs(that.data.startDate).startOf('day').format('YYYY-MM-DD HH:mm:ss'), //开始时间
      endTime: dayjs(that.data.endDate).endOf('day').format('YYYY-MM-DD HH:mm:ss'), //结束时间
      keyWord: that.data.search, //物料代码 or 唯⼀码
      pageId: 1, //⻚数
      pageSize: 200, //分⻚⼤⼩
    }).then((data) => {
      if (data.code == 0) {
        if (data.result.reportDTOs) {
          that.setData({
            proReportList: data.result.reportDTOs
          })
        } else {
          that.setData({
            proReportList: []
          })
        }
      }
    })
  },
  // 选择时间
  selectDate() {
    this.setData({
      isdaterange: true
    })
  },
  onAffirm(data) {
    console.log(data)
    if (data.result) {
      this.setData({
        startDate: dayjs(data.startDate).format('YYYY/MM/DD'),
        endDate: dayjs(data.endDate).format('YYYY/MM/DD')
      })
      // 重新获取数据
      this.productionReportPageQuery();
    }
    this.setData({
      isdaterange: false
    })
  },
  // 搜索
  onInputSearch(e) {
    let that = this
    clearTimeout(that.data.timeOut)
    that.data.search = e.detail.value
    that.data.timeOut = setTimeout(function () {
      that.productionReportPageQuery()
    }, 1000);
  },

  // 展开
  onUnfold(e) {
    let index = e.target.dataset.index
    this.data.proReportList[index].isUnfold = !this.data.proReportList[index].isUnfold
    this.setData({
      proReportList: this.data.proReportList
    })
  },

});
